package util;

import java.util.StringTokenizer;

import exception.FormatException;
import exception.InvalidCarNumException;
import exception.SpecialCharacterException;

public class CheckExceptionHelper {

	public static void isValidDate(String str) throws SpecialCharacterException {

		boolean isValid = true;
		int slashCount = 0;

		try{
		
		// 자릿수 검사.
		if(str.length() != 10)
			isValid = false;

		// 슬래쉬 갯수 검사.
		for(int i = 0; i < str.length(); ++i) {
			if(str.charAt(i) == '/')
				++slashCount;
		}
		
		} catch(NullPointerException e) {
			throw new SpecialCharacterException();
		}

		if(slashCount != 2)
			isValid = false;

		if(!isValid)
			throw new SpecialCharacterException();
			
	}

	public static void isValidTime(String str) throws SpecialCharacterException {

		boolean isValid = true;
		int dotCount = 0;

		try{
		
		// 자릿수 검사.
		if(str.length() != 5) 
			isValid = false;

		// : 기호 검사.
		for(int i = 0; i < str.length(); ++i) {
			if(str.charAt(i) == ':')
				++dotCount;
		}

		} catch(NullPointerException e){
			throw new SpecialCharacterException();
		}
		
		if(dotCount != 1)
			isValid = false;

		if(!isValid)
			throw new SpecialCharacterException();

	}

	public static void isValidPhone(String str) throws FormatException {

		boolean isValid = true;
		String[] nums = new String[3];
		int i = 0;

		StringTokenizer stk = new StringTokenizer(str,"-");

		try{
		
		try{
			// '-' 기준으로 스트링 나눔.
			while(stk.hasMoreElements()) {
				nums[i] = stk.nextToken();
				++i;
			}

		} catch(ArrayIndexOutOfBoundsException e) {
			isValid = false;
			e.printStackTrace();
		}
		
		// 각 전화번호 부분 별로 자릿수 검사.
		if(nums[0].length() != 3)
			isValid = false;
		if(nums[1].length() != 4)
			isValid = false;
		if(nums[2].length() != 4)
			isValid = false;
	
		} catch(NullPointerException e) {
			throw new FormatException();
		}
		
		if(!isValid)
			throw new FormatException();
		
	}
	
	public static void isValidCarNum(String str) throws InvalidCarNumException {
		
		boolean isValid = true;
		
		String[] nums = new String[2];
		int i = 0;
		int digitCount = 0;

		StringTokenizer stk = new StringTokenizer(str," ");

		try{
		
		try{
			// 공백기준으로 나눔.
			while(stk.hasMoreElements()) {
				nums[i] = stk.nextToken();
				++i;
			}

		} catch(ArrayIndexOutOfBoundsException e) {
			isValid = false;
			e.printStackTrace();
		}
		
		// 각 자릿수 검사.
		if(nums[0].length() != 3)
			isValid = false;
		
		if(nums[1].length() != 4)
			isValid = false;
		
		String preNum = nums[0];
		String postNum = nums[1];
		
		// 숫자 갯수 검사.
		for(int j = 0; j < preNum.length(); ++j) {
			
			char ch = preNum.charAt(j);
		
			if(Character.isDigit(ch))
				++digitCount;
		}
		
		if(digitCount != 2)
			isValid = false;
		
		digitCount = 0;
		
		for(int j = 0; j < postNum.length(); ++j) {
			
			char ch = postNum.charAt(j);
		
			if(Character.isDigit(ch))
				++digitCount;
		}
		
		digitCount = 0;
		} catch(NullPointerException e) {
			throw new InvalidCarNumException();
		}
		
		if(!isValid)
			throw new InvalidCarNumException();
		
	}

}
